<template>
    <div class="app-container">
        <div>
            <p class="dialogTitle title">当前地震事件:
                <span v-if="currentEvent">{{ currentEvent.eventName }}</span>
                <span v-else>请在地震事件管理中设置当前地震事件</span>
            </p>
        </div>

        <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="100px" v-show="showSearch">
            <el-form-item label="报表类别" prop="reportType">
                <el-select v-model="queryParams.reportType" placeholder="请选择报表类别" clearable>
                    <el-option v-for="item in option" :key="item.dictItemCode" :label="item.dictItemName"
                        :value="item.dictItemCode">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="审核状态" prop="checkStatus">
                <el-select v-model="queryParams.checkStatus" placeholder="请选择审核状态" clearable>
                    <el-option
                        v-for="item in [{ label: '未审核', value: '1' }, { label: '确认', value: '2' }, { label: '退回', value: '3' }]"
                        :key="item.value" :label="item.label" :value="item.value">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="上报人员" prop="createUserName">
                <el-input v-model="queryParams.createUserName" placeholder="请输入上报人员" clearable />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="handleQuery">查询</el-button>
                <el-button @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" :disabled="!currentEvent || dataList.length == 0 || multiple"
                    @click="handleMoreAudie">审核</el-button>
            </el-col>
        </el-row>

        <el-table v-loading="loading" :data="dataList" stripe row-key="xitId" @selection-change="handleSelectionChange">
            <el-table-column type="selection" align="center" :reserve-selection="true" width="55" />
            <el-table-column label="序号" align="center" width="55">
                <template #default="scope">
                    <span>{{ (queryParams.page - 1) * queryParams.rows + scope.$index + 1 }}</span>
                </template>
            </el-table-column>
            <el-table-column label="报表类别" align="center" prop="reportType" show-overflow-tooltip>
                <template #default="scope">
                    <span style="color: #107ce1;cursor: pointer;" @click="handleDownload(scope.row)">{{ getReportType(scope.row.reportType)
                    }}</span>
                </template>
            </el-table-column>
            <el-table-column label="上报人员" align="center" prop="createUserName" />
            <el-table-column label="审核状态" align="center" prop="checkStatus">
                <!-- 审核状态0,1,2,3未提交，未审核，确认，退回 -->
                <template #default="scope">
                    <span v-if="scope.row.checkStatus == '1'">未审核</span>
                    <span v-if="scope.row.checkStatus == '2'">确认</span>
                    <span v-if="scope.row.checkStatus == '3'">退回</span>
                </template>
            </el-table-column>
            <el-table-column label="审核意见" align="center" prop="checkOpinion" />
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button type="text" @click="handleAudit(scope.row)">审核</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.page" v-model:limit="queryParams.rows"
            @pagination="getList" />

        <el-dialog title="审核管理" v-model="openAudit" width="600px" append-to-body>
            <el-form ref="auditRef" label-width="80px" :model="form1" :rules="rules">
                <el-row style="margin: 20px 0;">
                    <el-col>
                        <el-form-item label="审核结果" prop="status">
                            <el-select v-model="form1.status" style="width: 100%;">
                                <el-option v-for="item in [{ label: '通过', value: '2' }, { label: '不通过', value: '3' }]"
                                    :key="item.value" :label="item.label" :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col>
                        <el-form-item label="审核意见" prop="suggestion">
                            <el-input v-model="form1.suggestion" type="textarea" maxlength="500" show-word-limit
                                :autosize="{ minRows: 5, maxRows: 5 }" />
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="saveAudit">保 存</el-button>
                    <el-button @click="openAudit = false">取 消</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>
  
<script setup name="lossReportAudit">
import useEventStore from "@/store/modules/event";
import { getAllList, getDetail, auditSave } from "@/api/lossReportManage/lossReportOn.js"
import { getDict } from "@/api/system/dict";
const currentEvent = computed(() => useEventStore().currentEvent);
const { proxy } = getCurrentInstance();

const dataList = ref([]);
const ids = ref();
const loading = ref(false);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const showSearch = ref(true);
const option = ref([])
const isMorestatus = ref(false);
const openAudit = ref(false);
const selectstatus = ref([]);
const data = reactive({
    queryParams: {
        page: 1,
        rows: 10,
        reportType: undefined,
        checkStatus: undefined,
        createUserName: undefined,
    },
    form1: {},
    rules: {
        status: [{ required: true, trigger: "change", message: "审核结果不能为空" }],
        suggestion: [{ required: true, trigger: "blur", message: "审核意见不能为空" }],
    },
})
const { queryParams, form1, rules } = toRefs(data)

/** 多选框选中数据 */
function handleSelectionChange(selection) {
    single.value = selection.length != 1;
    multiple.value = !selection.length;
    ids.value = selection.map((item) => item.xitId);
    selectstatus.value = selection;
};

// 批量
function handleMoreAudie() {
    isMorestatus.value = true;
    openAudit.value = true;
    proxy.resetForm("auditRef");
    selectstatus.value = selectstatus.value;
    // form1.value.ids = ids.value.join(',');
}

// 审核
function handleAudit(row) {
    isMorestatus.value = false;
    openAudit.value = true;
    proxy.resetForm("auditRef");
    // form1.value.ids = row.id;
    selectstatus.value = row;
}
// 保存审核
function saveAudit() {
    proxy.$refs["auditRef"].validate(valid => {
        if (valid) {
            let params = null
            if (isMorestatus.value) {
                params = {
                    "checkOpinion": form1.value.suggestion,
                    "checkStatus": form1.value.status,
                    "ids": selectstatus.value.map(item => item.id).join(','),
                }
            } else {
                params = {
                    "checkOpinion": form1.value.suggestion,
                    "checkStatus": form1.value.status,
                    "ids": selectstatus.value.id,
                }
            }
            auditSave(params).then(res => {
                proxy.$modal.msgSuccess("审核完成");
                openAudit.value = false;
                getList();
            })
        }
    })
}


// 查看
function handleDownload(row) {
    getDetail({ id: row.id }).then(res => {
        const attachment = res.data.attachment
        location.href = import.meta.env.VITE_APP_BASE_API + "/attachment/downLoadFile?attachId=" + encodeURIComponent(attachment.attachId);
    })
}


/** 搜索按钮操作 */
function handleQuery() {
    queryParams.value.page = 1;
    getList();
}


/** 重置按钮操作 */
function resetQuery() {
    dataList.value = [];
    proxy.resetForm("queryRef");
    handleQuery();
}

/** 查询数据列表 */
function getList() {
    if (currentEvent.value) {
        loading.value = true;
        getAllList({ ...queryParams.value, xsemId: currentEvent.value.id, sign: '1,2,3' }).then(response => {
            dataList.value = response.data.rows ? response.data.rows : [];
            total.value = response.data.total;
            loading.value = false;
        });
    } else {
        dataList.value = [];
        total.value = 0;
    }
}
function getReportType(val){
  var name = ''
  option.value.forEach(item => {
		if(item.dictItemCode == val) {
			name = item.dictItemName
			return
		}
	})
	return name
}
/** 获取字典值 */
async function getDictData() {
    await getDict("bblb").then(response => {
        option.value = response.data;
    });
}
onMounted(() => {
    setTimeout(() => {
        getList();
    }, 200)
})
getDictData();
</script>
  
<style scoped lang="scss">
.title {
    margin: 0 0 20px !important;
}
</style>
  
  